package no.nordicsemi.android.blinky.utils;

import android.graphics.Bitmap;
import android.os.Build;

import java.util.ArrayList;

public class BitmapUtils {
    /**
     * 得到bitmap的大小
     */
    public static int getBitmapSize(Bitmap bitmap) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {    //API 19
            return bitmap.getAllocationByteCount();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR1) {//API 12
            return bitmap.getByteCount();
        }
        // 在低版本中用一行的字节x高度
        return bitmap.getRowBytes() * bitmap.getHeight();                //earlier version
    }


    /**
     * 将大数组拆分为多个小数组
     * @param superbyte  需要拆分原始数组
     * @param size    拆分后单个数组大小
     * @return
     */
    public static ArrayList<byte[]> SplitList(byte[] superbyte, int size) {
        ArrayList<byte[]> result = new ArrayList<>();
        int length = superbyte.length;
        int count = length / size;
        int r = length % size;
        for (int i = 0; i < count; i++) {
            byte[] newbyte = new byte[size];
            System.arraycopy(superbyte,size*i,newbyte,0,size);
            result.add(i,newbyte);
        }
        if (r != 0) {
            byte[] newbyte = new byte[r];
            System.arraycopy(superbyte,length-r,newbyte,0,r);
            result.add(newbyte);
        }
        return result;
    }

    /**
     * 拼接字节数组
     * @param data1
     * @param data2
     * @return
     */
    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data3 = data1;
        if (data2 != null && data2.length != 0){
            data3 = new byte[data1.length + data2.length];
            System.arraycopy(data1, 0, data3, 0, data1.length);
            System.arraycopy(data2, 0, data3, data1.length, data2.length);
        }
        return data3;
    }
}
